package cat.widget.acgmoe.acgmoewidget;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import cat.widget.acgmoe.acgmoewidget.Utils.PrintLog;

public class MainActivity extends AppCompatActivity {

    private WebView mainWebview;
    private ProgressBar progressBar;


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        /**
         * @param item  选择器选项
         * @return 返回是否选择对应项目
         */
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    LoadUrl(mainWebview, "https://acg12.com/category/pc-moe/android-moe/");
                    return true;
                case R.id.navigation_dashboard:
                    if (mainWebview.isEnabled())
                        LoadUrl(mainWebview, "http://www.acg12.net/?p=1324");
                    return true;
                case R.id.navigation_notifications:

                    LoadUrl(mainWebview, "https://acg12.com/291506/");

                    return true;
            }
            return false;
        }
    };

    /**
     * @param mainWebview
     * @param url
     */
    private void LoadUrl(WebView mainWebview, String url) {
        try {
            mainWebview.stopLoading();
            if (mainWebview != null)
                mainWebview.loadUrl(url);

        } catch (Exception ex) {
            PrintLog.Log(ex);
        }
    }

    /**
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        InitView();

    }

    /**
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {


        }
        return super.onKeyDown(keyCode, event);

    }

    /**
     * u初始化界面信息
     */
    private void InitView() {

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        mainWebview = (WebView) findViewById(R.id.main_webview);

        mainWebview.getSettings().setJavaScriptEnabled(true);    //允许加载javascript
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        mainWebview.setWebViewClient(new WebViewClient() {
            /**
             * @param view
             * @param url
             */
            @Override
            public void onPageFinished(WebView view, String url) {
                try {
                    super.onPageFinished(view, url);


                } catch (Exception ex) {
                    PrintLog.Log(ex);
                }

            }


            /**
             * @param view
             * @param url
             * @param favicon
             */
            @Override

            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            }

            /**
             * @param view
             * @param request
             * @param error
             */
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                PrintLog.Log(error.toString());

            }

            /**
             * @param view
             * @param url
             * @return
             */
            @Override

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LoadUrl(view, url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

        mainWebview.setWebChromeClient(new WebChromeClient() {
            /**
             * @param view
             * @param newProgress
             */
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) { // 网页加载完成
                    progressBar.setVisibility(View.GONE);//加载完网页进度条消失
                } else { // 加载中
                    progressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    progressBar.setProgress(newProgress);//设置进度值
                }
            }
        });
        LoadUrl(mainWebview, "https://xiazaigal.com/wp-content/uploads/2018/12/morejump.html");

    }

}
